infoController.$inject = ['infoService', '$rootScope', '$scope'];
var $ = require('jquery')

function infoController(infoService, $rootScope, $scope) {

    var vm = this;
    try{
        var merchant = JSON.parse(sessionStorage.m);
        vm.merchant = merchant;
    }catch (err){
        
    }

    vm.openModal = function () {
        $("#updateModal").modal('show');
    }

    //broadcast receive to refresh
    $scope.$on('verify', function (ev, v) {

        window.location.reload();
    })



    vm.update = function () {

        var form = document.getElementById("updateForm");
        var fd = new FormData(form);

        if(fd.get('storeName')==''||fd.get('address')==''){
            swal({
                title : "Please input again",
                text : 'not allow empty field',
                type : "error",
                timer : 1000,
                showConfirmButton : true,
                confirmButtonColor : "#66CD00",
            });
            return;
        }

        if(fd.get('idcard').name ==''||fd.get('img').name==''){
            swal({
                title : "Please select again",
                text : 'not choose your file',
                type : "error",
                timer : 1000,
                showConfirmButton : true,
                confirmButtonColor : "#66CD00",
            });
            return;
        }
        infoService.update(
            fd
        ).then(function (data) {
            $("#updateModal").modal('hide');
            var str = JSON.stringify(data.data);
            sessionStorage.m = str;
            $rootScope.$broadcast('update', data.data);
            vm.merchant.img = data.data.img + "?" + new Date().getTime();
            infoService.sendToAdmin(data.data);
            var socket_admin =io.connect('ws://10.222.29.179:3000')
            socket_admin.emit('new_verify', null);

        });
    }




}

module.exports = infoController;